/* **************************************************************
NAME: yemenbalancetable.ado 
CREATED: 26 February 2021
EDITED: 26 February 2021 
AUTHOR: callan.corcoran@kellogg.northwestern.edu
PURPOSE: Program to create balance tables for the Yemen project 
************************************************************** */

cap prog drop yemenbalancetable

prog def yemenbalancetable
syntax varlist [if/], 		/// Variables to be included in table 
	[filename(string)] 		/// File name (default is TableX)
	[foldername(string)]	/// Output folder name (default is Replication/Output)
	[title(string)] 		/// Table title 
	[footnote(string)] 		/// Footnote text 
	[winsorize] 			/// Winsorize variables 
	[balancevar(varlist)]	/// Balance variable (default is treatment)
	
// **************************************************************
// PREPARE TABLE INPUTS 	

// FOLDER NAME 
// 	Set default folder to Replication/Output if not specified 
if "`foldername'"=="" {
	loc foldername "${rep_output}" 
}

// FILE NAME 
// 	Set a default filename 
if "`filename'"=="" {
	loc filename "Table1"
}
if "`winsorize'"=="winsorize" {
	loc filename "`filename'_win"
}

// WINSORIZE 
//	Edit varlist to include winsorized variable names if "winsorize" selected 
if "`winsorize'"=="winsorize" {
    loc templist `varlist'
	loc varlist // Clear varlist 
	foreach var in `templist' {
		loc currvar = subinstr("`var'","_bsl","_win1_bsl",.) // Add "_win1" suffix 
		loc varlist `varlist' `currvar' // Add to varlist if "winsorize" is selected
	}
}

// BALANCE VARIABLE 
if "`balancevar'"=="eligible" {
	loc label1 "Eligible"
	loc label2 "Non-Eligible"
	loc label3 "p-Value: Non-Eligible vs Eligible"
}
if "`balancevar'"=="" {
    loc balancevar treatment
	loc label1 "Treatment"
	loc label2 "Control"
	loc label3 "p-Value: Control vs Treatment"
}

// IF-STATEMENT 
if "`if'"!="" {
    loc ifif "if `if'"
	loc andif "& `if'"
} 

// FOOTNOTES 
// 	Add footnotes about baseline values and additional controls
if "`winsorize'"=="winsorize" {
	loc footnote "`footnote' Variables winsorized at 1%."
}

// **************************************************************
// CREATE TABLE FRAMEWORK 

clear all
eststo clear
estimates drop _all

loc columns = 5 //Change number of columns

set obs 10
gen x = 1
gen y = 1

forval i = 1/`columns' {
	eststo col`i': qui reg x y
}

loc count = 1
loc countamt = `count' + 1

loc stats "" // Added scalars to be filled
loc varlabels "" // Labels for row vars to be filled

use "${rep_analysis}.dta", clear 

// **************************************************************
// FILL TABLE CELLS 

loc i = 0 // Start a counter 

foreach var in `varlist' {

	// 1. Full sample
	qui sum `var' `ifif'
		loc stat`count' = r(mean)
		loc sd = r(sd)
		loc stat`count' = string(`stat`count'',"%9.2f")	
		loc sd = " (" + string(`sd',"%9.2f") + ")"
		estadd loc stat`count' = "`stat`count''" + "`sd'" : col1
			
	// 2. Balance Variable 
	qui sum `var' if `balancevar' == 1 `andif'
		loc stat`count' = r(mean)
		loc sd = r(sd)
		loc stat`count' = string(`stat`count'',"%9.2f")	
		loc sd = " (" + string(`sd',"%9.2f") + ")"
		estadd loc stat`count' = "`stat`count''"  + "`sd'": col2
			

	// 3. Control
	qui sum `var' if `balancevar' == 0 `andif'
		loc stat`count' = r(mean)
		loc sd = r(sd)
		loc stat`count' = string(`stat`count'',"%9.2f")	
		loc sd = " (" + string(`sd',"%9.2f") + ")"
		estadd loc stat`count' = "`stat`count''"  + "`sd'": col3
			
	// 4. p-values (Comparison vs Balance Variable)
	qui reg `var' `balancevar' i.village `ifif', r 
		test `balancevar' = 0 
		local thisp = `r(p)'
		loc stat`count' = r(p)
		loc stat`count' = string(`stat`count'',"%9.2f")	
		estadd loc stat`count' = "`stat`count''" : col4
		
	// 5. Observations 
	qui sum `var' `ifif'
		loc stat`count' = r(N)
		loc stat`count' = string(`stat`count'',"%9.0f")	
		estadd loc stat`count' = "`stat`count''" : col5
	
	// Row Labels and locals update
	loc thisvarlabel: var lab `var'
	loc varlabels "`varlabels' "`thisvarlabel'" "
	loc stats "`stats' stat`count'"
	loc count = `count' + 1
}

// F-test for joint significance 
qui reg `balancevar' `varlist' i.village `ifif', r 
test `varlist'

local thisp = `r(p)'
loc stat`count' = r(p)
loc stat`count' = string(`stat`count'',"%9.2f")	
estadd loc stat`count' = "`stat`count''" : col1
local varlabels "`varlabels' "F-test for joint significance" "
loc stats "`stats' stat`count'"


//***********************************************************
// EXPORT TABLE 

cd "`foldername'" // Call output folder directory 

esttab col* using "`filename'.csv",  title("`title'") cells(none) ///
	mtitle( "Full Sample" "`label1'" "`label2'" ///
	"`label3'" ///
	"Total N") stats(`stats', labels(`varlabels')) ///
	note("`footnote'")  ///
	compress wrap lines nonum replace plain

eststo clear

end 

